package com.lantosec.hmrp.modules.eval.controller;


import com.lantosec.hmrp.core.kit.PageVO;
import com.lantosec.hmrp.core.log.ControllerLog;
import com.lantosec.hmrp.core.log.LogType;
import com.lantosec.hmrp.core.util.UUIDUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import com.lantosec.hmrp.core.kit.Result;
import com.lantosec.hmrp.modules.eval.entity.EvalItem;
import com.lantosec.hmrp.modules.eval.service.IEvalItemService;
import com.lantosec.hmrp.modules.eval.vo.EvalItemVO;

import java.util.Arrays;
import java.util.List;

import lombok.extern.log4j.Log4j2;
import org.springframework.web.bind.annotation.RestController;
import com.lantosec.hmrp.core.mvc.BaseController;

/**
 * 考评项目定义 控制器
 *
 * @author AutoCode
 * @since 2020-03-13
 */
@Api(value = "考评项目定义控制器", tags = "考评项目定义控制器")
@RestController
@RequestMapping("/eval/item")
@Log4j2
public class EvalItemController extends BaseController {
    @Autowired
    private IEvalItemService service;

    @GetMapping("all")
    @ApiOperation(value = "查询全部")
    public Result<List<EvalItemVO>> all() {
        return Result.ok(entity2VO(service.list()));
    }

    @GetMapping("query")
    @ApiOperation(value = "分页查询")
    public Result<PageVO<EvalItemVO>> page(@ApiParam(value = "分页对象", required = true) PageVO<EvalItem> page,
                                           @ApiParam(value = "关键字") @RequestParam(name = "name", required = false) String name,
                                           @ApiParam(value = "考评对象") @RequestParam(name = "evalTarget", required = false) Integer evalTarget,
                                           @ApiParam(value = "考评奖惩形式") @RequestParam(name = "rewardOrientation", required = false) Integer rewardOrientation,
                                           @ApiParam(value = "考评项目分类") @RequestParam(name = "evalTypeId", required = false) String evalTypeId) {
        PageVO<EvalItemVO> pageVO = service.getAll(page, name, evalTarget, rewardOrientation,evalTypeId);
        return Result.ok(pageVO);
    }

    @GetMapping("get/{evalTypeId}")
    @ApiOperation(value = "主键查询")
    public Result<List<EvalItemVO>> get(@ApiParam(value = "ID", required = true) @PathVariable(name = "evalTypeId") String evalTypeId) {
        Object[] s = evalTypeId.split(",");
        List<EvalItem> evalItemList = service.list(new QueryWrapper<EvalItem>().in(EvalItem.ColName.eval_type_id.toString(), s));
        return Result.ok(entity2VO(evalItemList));
    }

    @GetMapping("getById/{evalItemId}")
    @ApiOperation(value = "主键查询")
    public Result<List<EvalItemVO>> getById(@ApiParam(value = "ID", required = true) @PathVariable(name = "evalItemId") String evalItemId) {
        List<EvalItem> evalItemList = service.list(new QueryWrapper<EvalItem>().in(EvalItem.ColName.eval_item_id.toString(), evalItemId));
        return Result.ok(entity2VO(evalItemList));
    }


    @PostMapping("create")
    @ControllerLog(name = "新增考评项目定义", type = LogType.add)
    @ApiOperation(value = "新增数据")
    public Result<String> create(@ApiParam(value = "考评项目定义VO对象", required = true) @RequestBody @Validated EvalItemVO vo, BindingResult bindingResult) {
        validationHandler(bindingResult);
        EvalItem entity = vo.convertToEntity();
        entity.setEvalItemId(UUIDUtils.get());
        service.save(entity);
        return Result.ok(entity.getEvalItemId());
    }

    @PutMapping("update")
    @ControllerLog(name = "修改考评项目定义", type = LogType.update)
    @ApiOperation(value = "修改数据")
    public Result<Void> update(@ApiParam(value = "考评项目定义VO对象", required = true) @RequestBody EvalItemVO vo) {
        EvalItem entity = vo.convertToEntity();
        service.updateById(entity);
        return Result.ok();
    }

    @DeleteMapping(value = "delete/{ids}")
    @ControllerLog(name = "逻辑删除考评项目排序", type = LogType.delete)
    @ApiOperation("逻辑删除")
    public Result<Void> delete(@ApiParam(value = "ID列表,用逗号分隔", required = true) @PathVariable(name = "ids") String ids) {
        if (StringUtils.isNotEmpty(ids)) {
            service.removeByIds(Arrays.asList(ids.split(",")));
        }
        return Result.ok();
    }

    @PutMapping("updateOrder")
    @ControllerLog(name = "修改考评项目排序", type = LogType.update)
    @ApiOperation(value = "修改排序")
    public Result<Void> updateOrder(@ApiParam(value = "考评项目定义ID", required = true) @RequestParam(value = "id") String id,
                                    @ApiParam(value = "考评项目定义排序值", required = true) @RequestParam(value = "evalItemOrder", required = false) int evalItemOrder) {
        service.updateOrder(id, evalItemOrder);
        return Result.ok();
    }
}
